<style>
    .detailHide {
        border: none;
    }

    .deviceForm label {
        width: 120px;
    }

    .companyFormPhotos {
        /* margin-left:140px; */
        line-height: 38px;
        float: left;
        margin-right: 30px;
    }

    .companyFormPhotos img {
        cursor: pointer;
        height: 38px;
        width: auto;
        max-width: 200px;
    }
    .layui-form-label{
        width: 130px;
    }
    .layui-input-block{
        margin-left: 140px;
    }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                               <!-- <div class="layui-inline">
                                    <label class="layui-form-label w-auto">按分类：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="qualificationTypeId">
                                            <option value="">按分类</option>
                                            <option value="1">高新技术企业</option>
                                            <option value="2">ISO9001</option>
                                        </select>
                                    </div>
                                </div>-->
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">资质名称：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入资质名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button lay-event="batchDel" type="button" id="batchDel"
                                    class="layui-btn icon-btn layui-btn-danger">批量删除
                            </button>
                            <button id="qualificationBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="qualificationTable" lay-filter="qualificationTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="qualificationTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="detail">详情</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="qualificationForm">
    <form lay-filter="qualificationForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">企业资质名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入企业资质名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">资质类型</label>
            <div class="layui-input-block">
                <select name="qualificationTypeId" lay-verify="required">
                    <option value="1">ISO9001</option>
                    <option value="2">高新技术企业</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">资质照片</label>
            <div class="layui-input-block">
                <div id="qualificationUrl" class="companyFormPhotos"></div>
                <input name="qualificationUrl" type="hidden" class="layui-input" lay-verify="" id="enclosureUrl"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">资质有效开始期</label>
            <div class="layui-input-block">
                <input name="startTime" placeholder="请输入修改人" type="text" class="layui-input" lay-verify="required"
                       required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">资质有效终止期</label>
            <div class="layui-input-block">
                <input name="stopTime" placeholder="请输入资质类型" type="text" class="layui-input" lay-verify="required"
                       required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">认证机构</label>
            <div class="layui-input-block">
                <input name="certification" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">资质描述</label>
            <div class="layui-input-block">
                <input name="qualificationDescribe" placeholder="" type="text" class="layui-input" lay-verify="required"
                       required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">一句话简介</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布时间</label>
            <div class="layui-input-block">
                <input name="createTime" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <!--        <div class="layui-form-item text-right">
                    <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                    <button class="layui-btn" lay-filter="qualificationFormSubmit" lay-submit>保存</button>
                </div>-->
    </form>
</script>


<!-- 添加表单弹窗 -->
<script type="text/html" id="addQualificationForm">
    <form lay-filter="addQualificationForm" class="layui-form model-form" enctype="multipart/form-data">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">企业资质名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">资质类型</label>
            <div class="layui-input-block">
                <select name="qualificationTypeId"  id="qualificationTypeId" lay-verify="required">
                    <option value="请选择"></option>
       <!--             <option value="1">高新技术企业</option>
                    <option value="2">ISO9001</option>-->
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">资质照片</label>
            <div class="layui-input-block">
                <div id="devicePic_path" class="companyFormPhotos"></div>
                <div id="devicePicBtn" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传图片
                </div>
                <input name="qualificationUrl" type="hidden" class="layui-input" lay-verify="" id="qualificationUrl"/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">资质有效开始期</label>
            <div class="layui-input-block">
                <!--                <input name="startTime" placeholder="" type="text" class="layui-input date-icon" lay-verify="required" required id="startTime"/>-->
                <input name="startTime" class="layui-input date-icon" type="text"
                       placeholder="请选择时间" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">资质有效终止期</label>
            <div class="layui-input-block">
                <!--                <input name="stopTime" placeholder="" type="text" class="layui-input" lay-verify="required" required id="stopTime"/>-->
                <input name="stopTime" class="layui-input date-icon" type="text"
                       placeholder="请选择时间" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">认证机构</label>
            <div class="layui-input-block">
                <input name="certification" placeholder="请选择认证机构" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">资质描述</label>
            <div class="layui-input-block">
                <textarea id="qualificationDescribe" name="qualificationDescribe" placeholder="请输入" class="layui-textarea"></textarea>
                <!--                <textarea class="layui-textarea" id="qualificationDescribe" placeholder="请输入内容"  style="display: none"  name="qualificationDescribe" autocomplete="off"></textarea>-->
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">一句话简介</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入简介" type="text" class="layui-input" lay-verify="required"
                       required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布时间</label>
            <div class="layui-input-block">
                <input name="createTime" class="layui-input date-icon" type="text"
                       placeholder="请选择发布时间" autocomplete="off"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="qualificationFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<style>
    .layui-form-label {
        width: 100px;
    }
</style>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', "laydate", "layedit", "upload"], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var layedit = layui.layedit;


        // 渲染表格
        var insTb = table.render({
            elem: '#qualificationTable',
            url: config.base + 'enterprise/qualification/admin/qualification/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{type: 'numbers',title: '序号'}
                // , {field: 'id', sort: true, title: 'ID'}
                , {field: 'name', sort: true, title: '资质名称'}
                , {field: 'certification',  title: '认证机构'}
                , {field: 'remark', title: '描述'}
                , {field: 'statusDesc', title: '状态'}
                , {field: 'createTime', sort: true, title: '添加时间'}
                /*
                                ,{field: 'qualificationTypeId', sort: true, title: '资质类型'}
                                ,{field: 'qualificationUrl', sort: true, title: '资质照片'}
                                ,{field: 'qualificationDescribe', sort: true, title: '资质描述'}
                                ,{field: 'stopTime', sort: true, title: '资质有效终止期'}
                                ,{field: 'startTime', sort: true, title: '资质有效开始期'}*/
                , {align: 'center', toolbar: '#qualificationTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if (menuButton.indexOf("搜索") == -1) {
                    $('.layui-form.toolbar').remove();
                }
                if (menuButton.indexOf("修改") == -1) {
                    $("[lay-event='edit']").remove();
                }
                if (menuButton.indexOf("删除") == -1) {
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(qualificationTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') { //修改
                showDetailModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(qualificationTable)', function (obj) {
            table.reload('qualificationTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        form.render('select');

        // 显示编辑弹窗
        function showDetailModel(data) {
            admin.open({
                type: 1,
                area: ['1180px', '800px'],
                offset: '65px',
                title: '资质详情',
                content: $('#qualificationForm').html(),
                success: function () {
                    form.val('qualificationForm', data);
                    admin.req('enterprise/qualification/admin/qualification/getQualificationById', {id: data.id}, function (res) {
                        console.log(res.data)

                        $('#qualificationUrl').html('<img src="' + res.data.qualificationUrl + '" style="height: 35px;" />');
                    }, 'GET', false)
                }
            });
        }

        $('#qualificationBtnAdd').click(function () {
            showAddModel();
        });

        function showAddModel(data) {
            admin.open({
                type: 1,
                area: '700px',
                offset: '65px',
                title: '添加资质',
                content: $('#addQualificationForm').html(),
                success: function () {
                    form.val('addQualificationForm', data);
                    // 表单提交事件
                    form.on('submit(qualificationFormSubmit)', function (d) {

                        d.field.qualificationDescribe = $('#qualificationDescribe').val()
                        layer.load(2);
                        admin.req('enterprise/qualification/admin/qualification/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('qualificationTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                        return false;
                    });
                }
            });

            //时间范围
            laydate.render({
                elem: 'input[name="startTime"]',
                type: "datetime",
                format: 'yyyy-MM-dd HH:mm:ss'
            });
            laydate.render({
                elem: 'input[name="stopTime"]',
                type: "datetime",
                format: 'yyyy-MM-dd HH:mm:ss'
            });
            laydate.render({
                elem: 'input[name="createTime"]',
                type: "datetime",
                format: 'yyyy-MM-dd HH:mm:ss'
            });
            //上传头像
            $('#devicePicBtn').click(uploadImg('devicePicBtn', 'devicePic_path', 'qualificationUrl'))
            // option渲染
            function loadSelectHtml(data, tips, htmltarget, type) {
                let html = ' <option value>' + tips + '</option>';
                if (!data || data == null) {
                    return;
                }
                for (let i = 0; i < data.length; i++) {
                    if (type == 1) {
                        html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                    } else if (type == 2) {
                        html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                    } else if (type == 3) {
                        html += `<option value='${data[i].id}'>${data[i].warehouseName}</option>`;
                    }else if(type == 4){
                        html += `<option value='${data[i].id}'  skuCode='${data[i].skuCode}'>${data[i].skuName}</option>`;
                    } else if(type == 5){
                        html += `<option value='${data[i].id}'>${data[i].skuName}</option>`;
                    }
                }

                $("form select[name='" + htmltarget + "']").html(html);
                form.render('select');
            }
            //下拉框 渲染
            admin.req('enterprise/qualification/admin/qualificationType/getQualificationType', {}, function (res) {
                if (res.code == 200) {
                    loadSelectHtml(res.data, "请选择资质类型", "qualificationTypeId", 2);
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'GET');
        }



        // 上传文件
        function uploadImg(_elem, pathId, inputId) {
            upload.render({
                elem: '#' + _elem
                , url: config.upload_url + 'uploadFile'
                , data: {}
                , accept: 'file'
                , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                , headers: {Authorization: 'Bearer ' + config.getToken()}
                , done: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg || "上传成功", {icon: 1});
                        let imagUrl = res.data.url;
                        console.log(imagUrl)
                        $('#' + pathId).html('<img src="' + imagUrl + '"/>');
                        $('#' + inputId).val(imagUrl);
                    } else if (res.code == 502) {
                        return location.replace('admin/login.html');
                    } else {
                        layer.msg(res.msg || "上传失败", {icon: 2});
                    }
                }
            });
        }


        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/qualification/admin/qualification/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        //批量删除
        $('#batchDel').click(function () {
            //获取选中的数据
            var checkRows = table.checkStatus('qualificationTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要删除的数据', {icon: 2});
            } else {
                var ids = '';
                checkRows.data.forEach(item => {
                    ids += item.id + ','
                })
                delBatch(ids)
            }
        });

        function delBatch(ids) {

            admin.req('enterprise/qualification/admin/qualification/delBatch', {ids: ids}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('qualificationTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        // 导出excel
        $('#qualificationBtnExport').click(function () {
            var checkRows = table.checkStatus('qualificationTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>